#!/bin/bash
# 安装mysql5.7.30
# 判断是否为root用户，不是root用户则退出
if [ ${USER} != "root" ]; then
	echo "权限不足"
	exit
else
	echo "欢迎光~~临！"
fi

# 软件包存放位置
packages_dir="/usr/local/src/tar_packages"
# 依赖
cmake="cmake-3.17.3"
bison="bison-3.5.1"
boost="boost_1_59_0"
mysql="mysql-5.7.30"

# 下载路径
cmake_url="10.10.0.12/packages/cmake-3.17.3.tar.gz"
bison_url="10.10.0.12/packages/bison-3.5.1.tar.gz"
boost_url="10.10.0.12/packages/boost_1_59_0.tar.gz"
mysql_url="10.10.0.12/packages/mysql-5.7.30.tar.gz"

# 安装依赖
depandence_install(){
if [ -d /usr/local/src/tar_packages ]; then
	echo "nothing"
else
	mkdir /usr/local/src/tar_packages
fi

# 配置yum源
if [ -f /etc/yum.repos.d/CentOS-Base.repo -a -f /etc/yum.repos.d/epel.repo ]; then
	echo "镜像存在"
else	
	rm -rf /etc/yum.repos.d/*
	curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
	curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
	yum clean all
	yum makecache
fi

# 安装开发者工具
yum -y groupinstall "Development Tools"
yum -y install openssl openssl-devel wget vim net-tools ncurses ncurses-devel 
yum -y upgrade

# 安装cmake依赖
cmake --version
if [ $? -eq 0 ]; then
	echo "cmake已安装"
else
	wget -P ${packages_dir} ${cmake_url}
	tar zxvf ${packages_dir}/${cmake}.tar.gz -C ${packages_dir}/../
	cd ${packages_dir}/../${cmake}
	./bootstrap && gmake && gmake install
	if [ $? -eq 0 ]; then
		echo "cmake安装完成"
	else
        	echo "失败，请查看日志"
		exit
	fi
	ln -s ${packages_dir}/${cmake}/bin/cmake /usr/bin/cmake
	if [ $? -eq 0 ]; then
		echo "cmake安装成功"
	fi
fi

# 安装bison依赖
if [ -d /usr/local/bison ]; then
	echo "bison已安装"
else
	wget -P ${packages_dir} ${bison_url}
	tar zxvf ${packages_dir}/${bison}.tar.gz -C ${packages_dir}/../
	cd ${packages_dir}/../${bison}
	./configure --prefix=/usr/local/bison && make && make install
	if [ $? -eq 0 ]; then
        	echo "bison安装成功"
	else
	        echo "失败，请查看日志"
		exit
	fi
fi

# 安装boost依赖
if [ -d /usr/local/boost ]; then
	echo "boosts已安装"
else
	wget -P ${packages_dir} ${boost_url}
	tar zxvf ${packages_dir}/${boost}.tar.gz -C /usr/local/
	mv /usr/local/${boost} /usr/local/boost
	if [ $? -eq 0 ]; then
        	echo "boost安装成功"
	else
        	echo "失败，请查看日志"
		exit
	fi
fi

# 检查系统是否自带mariadb，删除避免冲突
rpm -qa | grep mariadb
if [ $? -eq 0 ]; 
then
	rpm -e mariadb-libs --nodeps
fi
}

# 编译安装mysql
mysql_install(){
id mysql
if [ $? -ne 0 ]; then
	echo "没有这个用户,创建一个！"
	groupadd -r mysql
	useradd -r -g mysql -M mysql
	id mysql
fi

if [ -d /usr/local/mysql -a -f /usr/lib/systemd/system/mysqld.service ]; then
	echo "mysql已安装"
else
	mkdir /usr/local/mysql
	if [ -e ${mysql}.tar.gz ]; then
		echo "不下载啦啦啦啦啦"
	else
		wget -P ${packages_dir} ${mysql_url}
	fi
	tar -xzvf ${packages_dir}/${mysql}.tar.gz -C ${packages_dir}/../
	cd ${packages_dir}/../${mysql}
	cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
	-DMYSQL_DATADIR=/usr/local/mysql/data \
	-DSYSCONFDIR=/usr/local/mysql/etc \
	-DWITH_BOOST=/usr/local/boost \
	-DDEFAULT_CHARSET=utf8 \
	-DDEFAULT_COLLATION=utf8_general_ci \
	-DEXTRA_CHARSETS=all \
	-DMYSQL_UNIX_ADDR=/usr/local/mysql/tmp/mysql.sock \
	-DWITH_MYISAM_STORAGE_ENGINE=1 \
	-DWITH_INNOBASE_STORAGE_ENGINE=1 \
	-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
	-DWITH_PARTITION_STORAGE_ENGINE=1 \
	-DWITH_SYSTEMD=1
	make && make install
	if [ $? -eq 0 ]; then
        	echo "mysql安装成功"
	else
	        echo "失败，请查看日志"
		exit
	fi
	echo 'export PATH=$PATH:/usr/local/mysql/bin'>>/etc/profile
	source /etc/profile

	ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
	if [ -L /usr/bin/mysql ]; then
		echo "软链接已建立"
	else
		echo "软链接创建失败，请查看是否已存在"
	fi

	mkdir -p /usr/local/mysql/{log,data,tmp,etc}
	chown -R mysql:mysql /usr/local/mysql
	chmod 750 /usr/local/mysql/data
	cd /usr/local/mysql
	touch /usr/local/mysql/etc/my.cnf

	echo "[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port=3306
server_id=1
socket=/usr/local/mysql/tmp/mysql.sock
pid-file=/usr/local/mysql/tmp/mysql.pid
log-error=/usr/local/mysql/data/mysqld.err" > /usr/local/mysql/etc/my.cnf

	mysqld --defaults-file=/usr/local/mysql/etc/my.cnf --initialize-insecure --user=mysql --basedir=/usr/local/mysql  --explicit_defaults_for_timestamp
	cp -f /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/mysqld.service
	sed -i "s#^PIDFile=.*#PIDFile=/usr/local/mysql/tmp/mysqld.pid#" /usr/lib/systemd/system/mysqld.service
	sed -i "s#^ExecStart=.*#ExecStart=/usr/local/mysql/bin/mysqld --daemonize --pid-file=/usr/local/mysql/tmp/mysqld.pid#" /usr/lib/systemd/system/mysqld.service
	systemctl daemon-reload
	systemctl start mysqld
	systemctl enable mysqld
	mysql -e "ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';"
	mysql -uroot -p123456 -e "grant all privileges on *.* to 'root'@'%' identified by '123456';"
fi
}

depandence_install
mysql_install
echo "mysql安装成功！！！"